var AlarmCountMachineChart = function () {
  var template = /* html */ `
    <div class="alarm-count-machine-chart">
      <div ref="chart"></div>
    </div>
  `;

  return {
    props: {
      alarmData: {
        type: Object,
        required: true
      },
      pagination: {
        type: Object,
        required: true
      }
    },

    watch: {
      changeData: function () {
        this.resetChart();
      }
    },

    computed: {
      changeData: function () {
        var alarmData = this.alarmData;
        var pagination = this.pagination;
        return [alarmData.machineAlarmDataIds, pagination.currentPage];
      }
    },

    data: function () {
      return {};
    },

    mounted: function () {
      this.showChart();

      var self = this;
      $(this.$el).resize(function () {
        var chartDom = self.$refs['chart'];
        if (chartDom === undefined) {
          return;
        }
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart !== undefined) {
          chart.resize();
        }
      });
    },

    methods: {
      getChart: function () {
        var chartDom = this.$refs['chart'];
        var chart = echarts.getInstanceByDom(chartDom);
        if (chart === undefined) {
          chart = echarts.init(chartDom);
        }
        return chart;
      },

      showChart: function () {
        var chart = this.getChart();
        var chartOption = this.getChartOption();
        this.resetChartOption(chartOption);
        chart.setOption(chartOption);
      },

      resetChart: function () {
        var chart = this.getChart();
        var chartOption = {};
        this.resetChartOption(chartOption);
        chart.setOption(chartOption);
      },

      getChartOption: function () {
        var chartOption = {
          color: window.boardChartColor,
          grid: {
            right: 40,
            left: 50,
            bottom: 35,
            top: 40
          },
          legend: {
            type: 'scroll',
            top: 5,
            selectedMode: false,
            textStyle: {
              color: '#44D3E4',
              fontSize: 16
            },
            pageTextStyle: {
              color: '#44D3E4',
              fontSize: 16
            },
            formatter: function (name) {
              var label = i18n.getMsg(`remoteBoard.alarmCount.field.${name}`);
              return label;
            }
          },
          xAxis: [{
            type: 'category',
            boundaryGap: false,
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12,
              interval: 0,
              formatter: function (value, index) {
                if (typeof value !== 'string') {
                  return;
                }

                if (index % 2 === 1) {
                  value = '\n' + value;
                }
                return value;
              }
            }
          }],
          yAxis: [{
            type: 'value',
            min: 0,
            max: 5,
            name: '警报次数',
            nameGap: 10,
            axisLine: {
              lineStyle: {
                color: '#fff'
              }
            },
            axisLabel: {
              color: '#fff',
              fontSize: 12
            }
          }],
          tooltip: {
            trigger: 'axis',
            formatter: function (params) {
              var name = params[0].name;
              if (typeof name === 'number') {
                return;
              }

              var data = params[0].data;
              var texts = [name];
              for (var i = 0; i < params.length; i++) {
                var param = params[i];
                var seriesName = param.seriesName;
                var value = data[seriesName];
                var nameStr = i18n.getMsg(`remoteBoard.alarmCount.field.${seriesName}`);
                texts.push(`${param.marker} ${nameStr}:${value}次`);
              }

              return texts.join('<br>');
            }
          },
          dataset: {
            source: []
          },
          series: [{
            type: 'line',
            name: 'monthCount',
            encode: {
              x: 'name',
              y: 'monthCount'
            }
          }, {
            type: 'line',
            name: 'yearCount',
            encode: {
              x: 'name',
              y: 'yearCount'
            }
          }]
        };

        return chartOption;
      },

      resetChartOption: function (chartOption) {
        var alarmData = this.alarmData;

        _.set(chartOption, 'yAxis[0].max', alarmData.maxAlarmCount);

        var chartData = alarmData.getMachineChartData(this.pagination);
        _.set(chartOption, 'dataset.source', chartData);
      }
    },

    template: template
  };
}();
